package com.xiangban.yanglaodajie_admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_admin.dao.GoodsMapper;
import com.xiangban.yanglaodajie_admin.entity.Goods;
import com.xiangban.yanglaodajie_admin.entity.dto.GoodsDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;


/**
 * @Author Mr. MaYaYun
 * @Data 2021/6/4 14:58
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;

    public void addOrUpdateGoods(Goods goods) {// *商品id
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        goods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (BizEmpty(goods.getName())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getLogo())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getPrice())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getInventory())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getSort())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getClassifyId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goods.getAge())){
            goods.setAge(0);
        }
        if (goods.getGoodsId() == null){

            goodsMapper.saveGoods(goods);
        }else {
            goodsMapper.updateGoods(goods);
        }
    }

    public void delGoods(GoodsDto goodsDto) {//参数：商品id、商品状态
        if (BizEmpty(goodsDto.getGoodsIds())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(goodsDto.getStatus())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        goodsMapper.updateGoodsByGoodsId(goodsDto);
    }

    public PageInfo<GoodsDto> goodsList(Goods goods) {//参数：*商家id
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        goods.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<GoodsDto> goodsList = goodsMapper.findGoodsList(goods);
        return new PageInfo<>(goodsList);
    }


    public Object getGoodsInfo(Goods goods) {
        if (BizEmpty(goods.getGoodsId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        return goodsMapper.findGoodsByGoodsId(goods);
    }
}
